<?php
Class UserInfo extends Model
{
	public function __construct()
	{
		parent::Model();
	}
	
	public function AddUser()
	{
		$now=date("Y-m-d H:i:s");
		$data=array(
		'name'=>$this->input->xss_clean($this->input->post('name')),
		'password'=>$this->input->xss_clean($this->input->post('password')),
		'Role'=>'Normal',
		'gender'=>$this->input->post('gender'),
		'email'=>$this->input->post('email'),
		'address'=>$this->input->post('address'),
		'phone'=>$this->input-post('phone'));
		
		try 
		{
			$this->db->insert('userbasicinfo',$data);
		} catch (Exception $e) 
		{
			return 0;
		}
		
		return 1;
	}
	
	public function VerifyUser()
	{
		$name=$this->input->xss_clean($this->input->post('name'));
		$password=$this->input->xss_clean($this->input->post('password'));
		$sql="SELECT ID,UserName,Role FROM userbasicinfo where UserName='$name' and Password='$password'";
		try 
		{
			$query=$this->db->query($sql);
			if ($query->num_rows()>0)
			{
				$row=$query->row();
				if ($row->Role=='Admin') return 2;
				else
					return 1;
			}
		} catch (Exception $e) 
		{
			return 0;
		}
		
		return 0;
	}
	
	public function DeleteUser()
	{
		$this->db->trans_start();
		if ($this->DeleteUserData()==0)
			return 0;
		//get uid
		$sql="Delete From userbasicinfo where ID=$uid";
		try 
		{
			$query=$this->db->qeury($sql);
			if ($query->num_rows>0)
			{
				$this->db->trans_complete();
				return 1;
			}
		} catch (Exception $e) 
		{
			$this->db->trans_complete();
			return 0;
		}
		
		return 0;
	}
	
	public function DeleteUserData()
	{
		//get uid
		$this->db->trans_start();
		try 
		{
			$sql="select ID From paperbasicinfo where UserID=$uid";
			$query=$this->db->query($sql);
			foreach ($query->result() as  $pmrow)
			{
				$pbi=$pmrow->ID;
				$subsql="select ID From papers where PaperBasicID=$pbi";
				$subquery=$this->db->query($subsql);
				foreach ($subquery->result() as $psrow)
				{
					$psi=$psrow->ID;
					$ssubsql="Delete from questions where PaperID=$psi";
					$this->db->query($ssubsql);
				}
				$subsql="delete from papers where PaperBasicId=$pbi";
				$this->db->qeury($subsql);
			}
			$sql="delete from paperbasicInfo where UserID=$uid";
			$this->db-query($sql);
			
			$sql="select ID from questionbasicinfo where UserID=$uid";
			foreach ($query->result() as  $qbrow)
			{
				$qbi=$qbrow->ID;
				$subsql="delete From questionpool where QuestionID=$qbi";
				$this->db->query($subsql);
				$subsql="delete From questioncontentinfo where QuestionID=$qbi";
				$this->db->query($subsql);
				$subsql="delete From optionbasicinfo where QuestionID=$qbi";
				$this->db->query($subsql);
				$subsql="delete From answerbasicinfo where QuestionID=$qbi";
				$this->db->query($subsql);
				$subsql="select ID From parameterbasicinfo where QuestionID=$qbi";
				$subquery=$this->db->query($subsql);
				foreach ($subquery->result() as $pbrow)
				{
					$pbi=$pbrow->ID;
					$ssubsql="Delete from parametervalueinfo where ParameterID=$pbi";
					$this->db->query($ssubsql);
				}
				$subsql="delete from paramterbasicinfo where QuestionID=$qbi";
				$this->db->qeury($subsql);
			}
			$sql="delete from questionbasicinfo where UserID=$uid";
			$this->db-query($sql);
			
			$sql="Delete From categorybasicinfo where UserID=$uid";
			$query=$this->db->qeury($sql);
			
			$sql="Delete From taskbasicinfo where UserID=$uid";
			$query=$this->db->qeury($sql);
			
			$this->db->trans_complete();
		} catch (Exception $e) 
		{
			$this->db->trans_complete();
			return 0;
		}
		
		return 1;
	}
	
	public function UpdateUserInfo($Admin)
	{
		$uid=$this->input->post('id');
		$password=$this->input->xss_clean($this->input->post('password'));
		$gender=$this->input->post('gender');
		$email=$this->input->post('email');
		$address=$this->input->post('address');
		$phone=$this->input-post('phone');
		
		try 
		{
			$sql="update userbasicinfo set Password='$password',Gender='$gender',Email='$email',Address='$address',Phone='$phone' where ID=$uid";
			$this->db->query($sql);
		} catch (Exception $e) 
		{
			return 0;
		}
		
		return 1;
	}
}
?>